package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.Member;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 新增会员
     *
     * @param member
     * @return
     */
    @RequestMapping("/add")
    public Result insertMember(@RequestBody Member member) {
        try {
            memberService.insertMember(member);
        } catch (Exception e) {
            e.printStackTrace();
            //提示错误信息
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //提示成功消息
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 查询单项会员
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        Member member = memberService.findMemberById(id);
        if (member == null) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, member);
        }
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findMemberByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = memberService.findMemberByPage(queryPageBean);
        System.out.println(pageResult);
        return pageResult;
    }

    /**
     * 删除会员
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteMember")
    public Result deleteMember(Integer id) {
        try {
            memberService.deleteMember(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        //删除成功
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 修改会员
     *
     * @return
     */
    @RequestMapping("/updateMember")
    public Result updateMember(@RequestBody Member member) {
        try {
            memberService.updateMember(member);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑 修改失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        //编辑成功
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

}
